const electron = require('electron');
const {
    ipcRenderer,
    remote
} = electron;

var editor = ace.edit("editor");
//获取数据/更改主题/更改语言
ipcRenderer.send('data');
ipcRenderer.on('data-back', function (event, theme, mode) {
    editor.setTheme("ace/theme/" + theme);
    editor.getSession().setMode("ace/mode/" + mode);
    editor.setFontSize(18);
})

ipcRenderer.on('change-theme', function (event, data) {
    editor.setTheme("ace/theme/" + data);
})
ipcRenderer.on('change-mode', function (event, data) {
    editor.getSession().setMode("ace/mode/" + data);
})

//打开/关闭文件
ipcRenderer.on('open-file', function (event, message, path) {
    editor.setValue(message);
    console.log('open');
})
ipcRenderer.on('save-file', function (event) {
    ipcRenderer.send('save-data', editor.getValue());
    console.log('save');
})

ipcRenderer.on('save-data', function (event) {
    ipcRenderer.send('save-data-back', editor.getValue());
})


//文件拖动
const dragWrapper = document.getElementById("editor");
const fs = require('fs');
dragWrapper.addEventListener("drop",(event)=>{
    event.preventDefault(); //阻止e的默认行为
    const files = event.dataTransfer.files;
    if (files && files.length>=1){
        ipcRenderer.send('drag-path', files[0].path);
    }
})
//这个事件也需要屏蔽
dragWrapper.addEventListener("dragover",(e)=>{
    event.preventDefault();
})